import sqlite3

cdb_path = 'E:/YGO Cards/cdb/cards.cdb'

def link_cdb():
    connection = sqlite3.connect(cdb_path)

    if connection is None:
        print('连接失败')
    else :
        return connection.cursor()

def query_info(table_name, card_id, db_cursor):
    result = list(db_cursor.execute('SELECT * FROM ' + table_name + ' WHERE id=' + card_id))[0]

    id = str(result[0])
    name = result[1]
    desc = result[2].replace('\r\n', '<hr/>')

    card_info = {
        'CARDID' : id,
        'CARDNAME' : name,
        'CARDINFO' : desc
    }

    return card_info

def get_all_id(table_name, db_cursor):
    results = list(db_cursor.execute('SELECT id FROM ' + table_name))

    id_list = []

    for id in results:
        id_list.append(str(id[0]))

    return id_list

def write_to_html(card_info):
    file = open('./web_file/html/card_preview_template.html', 'r', encoding = 'UTF-8')

    html_source = '';

    for line in file.readlines():
        line = line.replace('{CARDID}', card_info['CARDID']).replace('{CARDNAME}', card_info['CARDNAME']).replace('{CARDINFO}', card_info['CARDINFO'])

        html_source += line

    file.close()

    file = open('./web_file/cards_preview/' + card_info['CARDID'] + '.html', 'w', encoding = 'UTF-8')

    file.write(html_source)
    file.flush()
    file.close()

    print(card_info['CARDID'] + '.html写入完成')

def write_all_id_to_html(table_name, id_list, cursor):
    for id in id_list:
        write_to_html(query_info(table_name, id, cursor))

def load_deck(deck_file_path):
    deck_file = open(deck_file_path, 'r', encoding = 'UTF-8')

    start = False

    main_cards = []
    extra_cards = []
    side_cards = []

    card_list = main_cards

    for line in deck_file.readlines():
        line = line.replace('\n', '').replace('\r\n', '')

        if line == '#extra':
            card_list = extra_cards
            continue
        elif line == '!side':
            card_list = side_cards
            continue

        if start:
            card_list.append(line)

        if line == '#main':
            start = True
            card_list = main_cards

    start = False

    if len(main_cards) <= 60 and len(main_cards) >= 40 and len(extra_cards) <= 15 and len(side_cards) <= 15:
        return main_cards, extra_cards, side_cards
    else :
        print('卡组数量不合法')

        return None, None, None

#<a href="http://www.baidu.com"><div style="background-image:url('../../cards/90448279.jpg');" class="card"></div></a>
def write_deck_html(file_name, main_cards, extra_cards, side_cards):
    main_cards_html = ''
    extra_cards_html = ''
    side_cards_html = ''

    for i in main_cards:
        main_cards_html += '<a href=\"../cards_preview/' + i + '.html"><div style=\"background-image:url(\'../../cards/' + i +'.jpg\');\" class=\"card\"></div></a>\n'

    for i in extra_cards:
        extra_cards_html += '<a href=\"../cards_preview/' + i + '.html"><div style=\"background-image:url(\'../../cards/' + i + '.jpg\');\" class=\"card\"></div></a>\n'

    for i in side_cards:
        side_cards_html += '<a href=\"../cards_preview/' + i + '.html"><div style=\"background-image:url(\'../../cards/' + i +'.jpg\');\" class=\"card\"></div></a>\n'

    template_file = open('web_file/html/deck_preview_template.html', 'r', encoding = 'UTF-8')

    html = ''

    for line in template_file.readlines():
        line = line.replace('{DECKNAME}', file_name).replace('{MAIN_CARDS}', main_cards_html).replace('{EXTRA_CARDS}', extra_cards_html).replace('{SIDE_CARDS}', side_cards_html)

        html += line

    template_file.close()

    output_file = open('./web_file/decks_preview/' + file_name + '.html', 'w', encoding = 'UTF-8')
    output_file.write(html)
    output_file.flush()
    output_file.close()

if __name__ == '__main__':
   # cursor = link_cdb()
   # id_list = get_all_id('texts', cursor)
   # write_all_id_to_html('texts', id_list, cursor)
   #
   # print('全部写入完成')

   main_cards, extra_cards, side_cards = load_deck('./deck/deck.ydk')
   write_deck_html('最强的闪刀姬', main_cards, extra_cards, side_cards)

   print('写入成功')